/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*    This project was/is sponsored by RNP and FAPESP.
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import br.org.groupwareworkbench.core.bd.ObjectDAO;
import br.org.groupwareworkbench.core.framework.Collablet;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Table;

@Entity
@Table(name="News")
public class News implements Serializable {

    private static final long serialVersionUID = 4820172445307827729L;

    private static ObjectDAO<News, Long> DAO = new ObjectDAO<News, Long>(News.class);

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private Collablet collablet;

    // adicionado do código do original da AUN
    private String codNews;

    private String title;

    private String subTitle;

    @Column(/*columnDefinition="MEDIUMTEXT",*/ length=20480)
    private String body;

    private String lead;

    private String category;

    private String relevance;

    private String status;

    @Temporal(TemporalType.TIMESTAMP)
    private Date closeDate;

    @Temporal(TemporalType.TIMESTAMP)
    private Date publishDate;

    @Temporal(TemporalType.TIMESTAMP)
    private Date sendDate;

    public News(String title, String body) {
        this();
        this.title = title;
        this.body = body;
    }

    public News() {
        this.closeDate = new Date();
        this.publishDate = new Date();
        this.sendDate = new Date();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof News)) return false;
        News other = (News) o;

        Long otherId = other.getId();
        String otherTitle = other.getTitle();
        Collablet otherCollablet = other.getCollablet();

        return (id == null ? otherId == null : id.equals(otherId)) &&
                (title == null ? otherTitle == null : title.equals(otherTitle)) &&
                (collablet == null ? otherCollablet == null : collablet.equals(otherCollablet));
    }

    @Override
    public int hashCode() {
        return (id == null ? 0 : id.hashCode()) ^
                (title == null ? 0 : title.hashCode()) ^
                (collablet == null ? 0 : collablet.hashCode());
    }

    public void save() {
        DAO.save(this);
    }

    public void delete() {
        DAO.delete(this);
    }

    public static News findById(long id) {
        return DAO.findById(id);
    }

    public static News findByTitle(String title, Collablet collablet) {
        if (collablet == null) throw new IllegalArgumentException();
        if (title == null) throw new IllegalArgumentException();
        return DAO.query()
                .with("collablet", collablet)
                .with("title", title)
                .find();
    }

    public static List<News> list(Collablet collablet) {
        if (collablet == null) throw new IllegalArgumentException();
        return DAO.query().with("collablet", collablet).list();
    }

    private static final Pattern ESCAPE_HTML = Pattern.compile("<+[A-Za-z0-9.:!^#@&\\?\\*\\(\\)\\[\\]_%\\$+-/\\\"\'= ]+>");

    private static String escapeHtml(String str) {
        return str == null ? null :
            ESCAPE_HTML.matcher(str).replaceAll(" ").replaceAll(" +", " ").trim();
    }

    public Long getId() {
        return id;
    }

    // Deve ser usado apenas pelo VRaptor.
    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public String getEscapedTitle() {
        return escapeHtml(title);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public String getEscapedSubTitle() {
        return escapeHtml(subTitle);
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getEscapedBody() {
        return escapeHtml(body);
    }

    public String getCodNews() {
        return codNews;
    }

    public void setCodNews(String codNews) {
        this.codNews = codNews;
    }

    public String getLead() {
        return lead;
    }

    public void setLead(String lead) {
        this.lead = lead;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getRelevance() {
        return relevance;
    }

    public void setRelevance(String relevance) {
        this.relevance = relevance;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getCloseDate() {
        return closeDate == null ? null : (Date) closeDate.clone();
    }

    public void setCloseDate(Date closeDate) {
        this.closeDate = closeDate == null ? null : (Date) closeDate.clone();
    }

    public Date getPublishDate() {
        return publishDate == null ? null : (Date) publishDate.clone();
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate == null ? null : (Date) publishDate.clone();
    }

    public Date getSendDate() {
        return sendDate == null ? null : (Date) sendDate.clone();
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate == null ? null : (Date) sendDate.clone();
    }

    public Collablet getCollablet() {
        return collablet;
    }

    public void setCollablet(Collablet collablet) {
        this.collablet = collablet;
    }
}

